package bma.shop.service;

import org.springframework.transaction.annotation.Transactional;

import bma.shop.dao.OrderDao;
import bma.shop.domain.Order;
import bma.shop.domain.Product;

@Transactional
public class DefaultOrderService implements OrderService {

	private OrderDao orderDao;
	private ProductService productService;
	
	
	public Order newOrder() {
		Order order = new Order();
		return orderDao.saveOrder(order);
	}

	public void addProduct(Product product, long orderId) {
		//productService.saveProduct(product);
		Order order = getOrder(orderId);
		order.addProduct(product);
		orderDao.saveOrder(order);
	}

	public void removeProduct(Product product, long orderId) {
		Order order = getOrder(orderId);
		order.removeProduct(product);
		orderDao.saveOrder(order);
	}

	public Order getOrder(long orderId) {
		Order order = orderDao.loadOrder(orderId);
		if (order == null) {
			throw new ServiceException("No order with id: " + orderId);
		}
		return order;
	}

	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	

}
